package com.android.fragmentdemo.util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Hashtable;

public class BitmapUtil {
    public static Bitmap createQRCode(String url, int qrCodeWidth, int qrCodeHeight) throws WriterException {
        int PADDING_SIZE_MIN = 6;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix matrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        boolean isFirstBlackPoint = false;
        int startX = 0;
        int startY = 0;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    if (!isFirstBlackPoint) {
                        isFirstBlackPoint = true;
                        startX = x;
                        startY = y;
                    }
                    pixels[y * width + x] = Color.BLACK;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //剪切中间的二维码区域，减少padding区域
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        if (startX <= PADDING_SIZE_MIN) {
            return bitmap;
        }
        int x1 = startX - PADDING_SIZE_MIN;
        int y1 = startY - PADDING_SIZE_MIN;
        if (y1 < 0) {
            return bitmap;
        }
        int w1 = width - x1 * 2;
        int h1 = height - y1 * 2;
        return Bitmap.createBitmap(bitmap, x1, y1, w1, h1);
    }

    public static Bitmap createQRCodeWithLogo(String url, int qrCodeWidth, int qrCodeHeight, Bitmap iconBitmap) throws WriterException {
        int PADDING_SIZE_MIN = 6;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix matrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        boolean isFirstBlackPoint = false;
        int startX = 0;
        int startY = 0;

        Matrix transMatrix = new Matrix();

        int scaleWidth = qrCodeWidth / 8;

        int halfQrCodeWidth = width / 2;
        int halfQrCodeHeight = height / 2;

        int iconBmpWidth = iconBitmap.getWidth();
        int iconBmpHeight = iconBitmap.getHeight();

        float sx = (float) 2 * scaleWidth / iconBmpWidth;
        float sy = (float) 2 * scaleWidth / iconBmpHeight;
        transMatrix.setScale(sx, sy);

        Bitmap targetBitmap = Bitmap.createBitmap(iconBitmap, 0, 0, iconBmpWidth, iconBmpHeight, transMatrix, false);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (x > halfQrCodeWidth - scaleWidth && x < halfQrCodeWidth + scaleWidth && y > halfQrCodeHeight - scaleWidth && y < halfQrCodeHeight + scaleWidth) {
                    //该位置用于存放图片信息
                    //记录图片每个像素信息
                    pixels[y * width + x] = targetBitmap.getPixel(x - halfQrCodeWidth + scaleWidth, y - halfQrCodeHeight + scaleWidth);
                } else {
                    if (matrix.get(x, y)) {
                        if (!isFirstBlackPoint) {
                            isFirstBlackPoint = true;
                            startX = x;
                            startY = y;
                        }
                        pixels[y * width + x] = Color.BLACK;
                    }
                }
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //剪切中间的二维码区域，减少padding区域
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        if (startX <= PADDING_SIZE_MIN) {
            return bitmap;
        }

        int x1 = startX - PADDING_SIZE_MIN;
        int y1 = startY - PADDING_SIZE_MIN;
        if (y1 < 0) {
            return bitmap;
        }

        int w1 = width - x1 * 2;
        int h1 = height - y1 * 2;
        return Bitmap.createBitmap(bitmap, x1, y1, w1, h1);
    }

    public static Bitmap createQRCodeWithLogo(String url, int size, Bitmap iconBmp) {
        try {
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, size, size, hints);
            //矩阵高度
            int width = bitMatrix.getWidth();
            //矩阵宽度
            int height = bitMatrix.getHeight();
            int halfW = width / 2;
            int halfH = height / 2;
            Matrix matrix = new Matrix();
            int scaleWidth = size / 8;
            float sx = (float) 2 * scaleWidth / iconBmp.getWidth();
            float sy = (float) 2 * scaleWidth / iconBmp.getHeight();
            matrix.setScale(sx, sy);
            //设置缩放信息
            //将logo图片按martix设置的信息缩放
            Bitmap targetBmp = Bitmap.createBitmap(iconBmp, 0, 0, iconBmp.getWidth(), iconBmp.getHeight(), matrix, false);
            int[] pixels = new int[size * size];
            for (int y = 0; y < size; y++) {
                for (int x = 0; x < size; x++) {
                    if (x > halfW - scaleWidth && x < halfW + scaleWidth && y > halfH - scaleWidth && y < halfH + scaleWidth) {
                        //该位置用于存放图片信息
                        //记录图片每个像素信息
                        pixels[y * width + x] = targetBmp.getPixel(x - halfW + scaleWidth, y - halfH + scaleWidth);
                    } else {
                        if (bitMatrix.get(x, y)) {
                            pixels[y * size + x] = 0xff000000;
                        } else {
                            pixels[y * size + x] = 0xffffffff;
                        }
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }
}
